<?php
/**
 *
 * User: ZF
 * Date: 2021/9/1 0001
 * Time: 10:41
 */

namespace app\backend\controller;


use app\backend\model\RoleModel;
use app\backend\service\AdminService;
use app\backend\service\MerchantService;
use app\common\RootController;
use \app\backend\model\AdminModel as AdminModel;

class Admin extends Backend
{
    /**
     * 修改用户资料
     * @param AdminService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     */
    public function info(AdminService $service)
    {
        if ($this->request->isAjax()) {
            $infoResult = $service->modify($this->request->param());
            return $this->success($infoResult);
        }
        return $this->fetch('', ['adminInfo' => $service->info()]);
    }

    /**
     * 重置密码
     * @param AdminService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     */
    public function resetPwd(AdminService $service)
    {
        if ($this->request->isAjax()) {
            $resetResult = $service->resetPwd($this->request->param());
            return $this->success($resetResult);
        }
        return $this->fetch();
    }

    /**
     * @param AdminService $service
     * @return string|\think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(AdminService $service)
    {
        if ($this->request->isAjax()) {
            return $this->success($service->lists($this->request->param()));
        }
        return $this->fetch();
    }

    /**
     * 新增用户
     * @param AdminService $service
     * @param RoleModel $roleModel
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     */
    public function add(AdminService $service)
    {
        if ($this->request->isAjax()) {
            $addResult = $service->add($this->request->param());
            return $this->success($addResult);
        }
        return $this->fetch('', ['roleList' => $service->roleList(), 'stateLabel' => AdminModel::$stateLabel]);
    }

    /**
     * 编辑用户
     * @param AdminService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(AdminService $service)
    {
        $params = $this->request->param();
        if ($this->request->isAjax()) {
            $editResult = $service->edit($params);
            return $this->success($editResult);
        }
        return $this->fetch('', ['info' => $service->detail($params['id']), 'roleList' => $service->roleList(),
            'stateLabel' => AdminModel::$stateLabel]);
    }

    /**
     * 用户详情
     * @param AdminService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(AdminService $service)
    {
        return $this->fetch('', ['info' => $service->detail($this->request->param('id')),
            'roleList' => $service->roleList(), 'stateLabel' => AdminModel::$stateLabel]);
    }

    /**
     * 修改用户状态
     * @param AdminService $service
     * @return \think\response\Json
     * @throws \app\common\RootException
     */
    public function changeState(AdminService $service)
    {
        return $this->success($service->changeState($this->request->param()));
    }

    /**
     * 用户删除
     * @param AdminService $service
     * @return \think\response\Json
     * @throws \app\common\RootException
     */
    public function del(AdminService $service)
    {
        $delResult = $service->del($this->request->param('id'));
        return $this->success($delResult);
    }
}